﻿using System;
using System.Collections.Generic;
using SDCloudSolution.SDCloud.BusinessLayer.Interfaces;
using SDCloudSolution.SDCloud.DataLayer.Interfaces;
using SDCloudSolution.SDCloud.Entities.Identity.OAuth;

namespace SDCloudSolution.SDCloud.BusinessLayer.Identity
{
    /// <summary>
    /// Implements a methods for works with <see cref="ClientApplicationEntity"/>.
    /// </summary>
    public class ClientApplicationComponent : IClientApplicationComponent
    {
        private IClientApplicationDataProvider _clientApplicationDataProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="ClientApplicationComponent"/> class.
        /// </summary>
        /// <param name="clientApplicationDataProvider">The client application data provider.</param>
        public ClientApplicationComponent(IClientApplicationDataProvider clientApplicationDataProvider)
        {
            _clientApplicationDataProvider = clientApplicationDataProvider;
        }

        /// <summary>
        /// Find a <see cref="ClientApplicationEntity" /> by specific client identifier.
        /// </summary>
        /// <param name="clientId">The client identifier.</param>
        public ClientApplicationEntity GetByClientId(string clientId)
        {
            return _clientApplicationDataProvider.GetByClientId(clientId);
        }

        /// <summary>
        /// Validates the client application credentials.
        /// </summary>
        /// <param name="clientId">The client identifier.</param>
        /// <param name="clientSecret">The client secret.</param>
        public bool ValidateApplication(string clientId, string clientSecret)
        {
            var clientApplication = GetByClientId(clientId);

            if (clientApplication == null)
            {
                return false;
            }

            if (!clientApplication.ClientSecret.Equals(clientSecret, StringComparison.InvariantCultureIgnoreCase))
            {
                return false;
            }

            if (!clientApplication.IsActive)
            {
                return false;
            }

            if (clientApplication.UserId == null)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Finds <see cref="ClientApplicationEntity"/>'s that attached to specific user.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public IList<ClientApplicationEntity> GetByUserId(int userId)
        {
            return _clientApplicationDataProvider.GetByUserId(userId);
        }

        /// <summary>
        /// Generates the application for user.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        public ClientApplicationEntity GenerateApplicationForUser(int userId)
        {
            var clientApplication = new ClientApplicationEntity
            {
                ClientId = Guid.NewGuid().ToString(),
                ClientSecret = Guid.NewGuid().ToString(),
                UserId = userId,
                IsActive = true,
                IsAutoGenerated = true,
                Name = "<Client::" + userId + ">"
            };

            return _clientApplicationDataProvider.Insert(clientApplication);
        }
    }
}
